package com.demo.iodemo;

import java.io.*;

/**
 * 生产文件
 */
public class CreateDataDemo {

    public static void main(String[] args) throws IOException {
//        createSpringBatchData();

        addData1();
//        addData();

    }

    private static void addData1() throws IOException {

        int num = 300000;
        String filePath = "D:\\springbatchdemo\\文件后再添加数据2.txt";
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();

        FileOutputStream out = new FileOutputStream(file);
        String a = "";
        // 给文件中生产30万条数据
        long beginTime = System.currentTimeMillis();
        for (int m = 0; m < 3; m++) {
            System.out.println("开始时间：【 " + beginTime + " 】");
            for (int i = 1; i <= num; i++) {
                a = "custNo_" + i + ",applicationNo_" + i + ",value1,value2,value3,value4,value5,value6,value7,value8,value9,value10,value11,value12,value13,value14,value15,value16,value17,value18,value19,value20\n";
                out.write(a.getBytes());
                out.flush();
            }
            System.out.println("总共耗时：【 " + (System.currentTimeMillis() - beginTime) + " 】毫秒");
        }
        out.close();


        // 在文件后再添加一部分数据
//        for (int n = 0; n < 3; n++) {
//            RandomAccessFile randomAccessFile = new RandomAccessFile(filePath, "rw");  // 权限
//            long fileLength = randomAccessFile.length();
//            randomAccessFile.seek(fileLength);
//            System.out.println("开始时间：【 " + beginTime + " 】");
//            for (int i = 1; i <= num; i++) {
//                a = "NewcustNo_" + i + ",applicationNo_" + i + ",value1,value2,value3,value4,value5,value6,value7,value8,value9,value10,value11,value12,value13,value14,value15,value16,value17,value18,value19,value20\n";
//                randomAccessFile.write(a.getBytes());
//            }
//            randomAccessFile.close();
//            System.out.println("总共耗时：【 " + (System.currentTimeMillis() - beginTime) + " 】毫秒");
//        }
    }

    /**
     * 对已有数据的文件后追加数据
     */
    private static void addData() throws IOException {

        int num = 30;
        String filePath = "D:\\springbatchdemo\\文件后再添加数据.txt";
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();

        FileOutputStream out = new FileOutputStream(file);
        String a = "";
        // 给文件中生产30万条数据
        long beginTime = System.currentTimeMillis();
        System.out.println("开始时间：【 " + beginTime + " 】");
        for (int i = 1; i <= num; i++) {
            a = "custNo_" + i + ",applicationNo_" + i + ",value1,value2,value3,value4,value5,value6,value7,value8,value9,value10,value11,value12,value13,value14,value15,value16,value17,value18,value19,value20\n";
            out.write(a.getBytes());
            out.flush();
        }
        out.close();
        System.out.println("总共耗时：【 " + (System.currentTimeMillis() - beginTime) + " 】毫秒");


        // 在文件后再添加一部分数据
        for (int n = 0; n < 3; n++) {
            RandomAccessFile randomAccessFile = new RandomAccessFile(filePath, "rw");  // 权限
            long fileLength = randomAccessFile.length();
            randomAccessFile.seek(fileLength);
            System.out.println("开始时间：【 " + beginTime + " 】");
            for (int i = 1; i <= num; i++) {
                a = "NewcustNo_" + i + ",applicationNo_" + i + ",value1,value2,value3,value4,value5,value6,value7,value8,value9,value10,value11,value12,value13,value14,value15,value16,value17,value18,value19,value20\n";
                randomAccessFile.write(a.getBytes());
            }
            randomAccessFile.close();
            System.out.println("总共耗时：【 " + (System.currentTimeMillis() - beginTime) + " 】毫秒");
        }
    }

    /**
     * 生产springbatch的测试数据
     *
     * @throws IOException
     */
    private static void createSpringBatchData() throws IOException {
        File file = new File("D:\\springbatchdemo\\", "data.txt");
        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();
        FileOutputStream out = new FileOutputStream(file);
        String a = "";
        // 给文件中生产30万条数据
        long beginTime = System.currentTimeMillis();
        System.out.println("开始时间：【 " + beginTime + " 】");
        for (int i = 1; i <= 300000; i++) {
            a = "custNo_" + i + ",applicationNo_" + i + ",value1,value2,value3,value4,value5,value6,value7,value8,value9,value10,value11,value12,value13,value14,value15,value16,value17,value18,value19,value20\n";
            out.write(a.getBytes());
            out.flush();
        }
        out.close();
        System.out.println("总共耗时：【 " + (System.currentTimeMillis() - beginTime) + " 】毫秒");
    }


}
